package com.springboot.demo.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springboot.demo.entity.Book;
import com.springboot.demo.dao.BookDao;

@Controller
public class BookController {

	@Autowired
	private BookDao bookDao;// 由于比较简单，省略了 service 层

	/**
	 * 4、删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/librarian_book_delete")
	public Map<String, Object> delete(@RequestParam(value = "ids") String ids) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String[] idsStr = ids.split(",");
		//for (int i = 0; i < ids.length; i++) { 
		for (String i:idsStr) {
			bookDao.deleteById(Integer.parseInt(i));
		}
		resultMap.put("success", true);

		return resultMap;
	}

	/**
	 * 3、添加、修改
	 * @param book
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/librarian_book_save")
	public Map<String, Object> save(Book book) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		bookDao.save(book);
		resultMap.put("success", true);
		return resultMap;
	}

//	@ResponseBody
//	@RequestMapping("/insert")
//	public Map<String, Object> insert(Book book) {
//		Map<String, Object> resultMap = new HashMap<String, Object>();
//		bookDao.insert(book);
//		resultMap.put("success", true);
//		return resultMap;
//	}


	/**
	 * 2、显示
	 * @param page
	 * @param rows
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/librarian_book_list")
	public Map<String, Object> list(Book book, 
		@RequestParam(value = "page", required = false) Integer page,
		@RequestParam(value = "rows", required = false) Integer rows
	) {
		Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, "id");
		Page<Book> pageBook = bookDao.findAll(new Specification<Book>() {

			@Override
			public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				Predicate predicate = cb.conjunction();
				if(book!=null){
					if(book.getId()!=null){
						predicate.getExpressions().add(cb.like(root.get("id"),"%"+book.getId()+"%"));
					}
					if(book.getBname()!=null){
						predicate.getExpressions().add(cb.like(root.get("bname"),"%"+book.getBname().trim()+"%"));
					}
				}
				return predicate;
			}

		}, pageable);
		// ①获取rows
		List<Book> list = pageBook.getContent();
		// ②获取count
		Long count = bookDao.count(new Specification<Book>() {

			@Override
			public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				Predicate predicate = cb.conjunction();
				if(book!=null){
					if(book.getId()!=null){
						predicate.getExpressions().add(cb.like(root.get("id"),"%"+book.getId()+"%"));
					}
					if(book.getBname()!=null){
						predicate.getExpressions().add(cb.like(root.get("bname"),"%"+book.getBname().trim()+"%"));
					}
				}
				return predicate;
			}

		});
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("rows", list);
		resultMap.put("total", count);
		resultMap.put("success", true);
		return resultMap;
	}
	/*
	@GetMapping("/deleted_book_list")
    public String deleted_book_list(Model model){
       // List<Book> deleteBook=deleteBookdao.getReturnMsg();		//引用上一层
        //----------------------------------------------需添加
        //List<Librarian> deleteLibrarian= ;						//同上
        model.addAttribute("book",deleteBook);
        model.addAttribute("librarian",deleteLibrarian)
        return "librarian_showdeletebook";
    }

	 */



	/**
	 * 1、进入增删改查界面
	 * @return
	 */
	@RequestMapping("/librarian_book")
	public String librarian_book() {
		return "librarian_book";
	}

	@RequestMapping("/librarian_homepage")
	public String librarian_homepage(){
		return "librarian_homepage";
	}

}
